package com.fita.admin.entity.search.data;

import com.demo2do.core.persistence.support.QueryBuilder;
import com.demo2do.core.persistence.support.QueryFilter;
import com.demo2do.core.persistence.support.QueryOrder;
import com.fita.entity.set.SetPayType;
import com.fita.entity.set.SetStatus;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;

/**
 * @author 11200
 *
 */
public class SetSearch implements QueryBuilder{

	private String code;
	
	private String country;
	
	private String supplier;
	
	private SetPayType payType;
	
	private SetStatus status;
	
    private QueryFilter queryFilter;

    private QueryOrder queryOrder;
    

	/* (non-Javadoc)
	 * @see com.demo2do.core.persistence.support.QueryBuilder#getPreparedFilter(boolean)
	 */
	public QueryFilter getPreparedFilter(boolean refresh) {

		if (queryFilter == null || refresh) {
			
			queryFilter = new QueryFilter();
			
			queryFilter.addLike("code", code);
			
			queryFilter.addEquals("payType", payType);
			
			queryFilter.addEquals("status", status);
			
			if (StringUtils.isNotBlank(country)) {
				
				country = "%" + country + "%";
				
				queryFilter.addSentence("country.englishName LIKE :country OR country.chineseName LIKE :country", "country", country);
			}
			if (StringUtils.isNotBlank(supplier)) {
				
				supplier = "%" + supplier + "%";
				
				queryFilter.addSentence("supplier.engliashName LIKE :supplier OR supplier.chineseName LIKE :supplier", "supplier", supplier);
			}
						
		}
		return queryFilter;
	}

	/* (non-Javadoc)
	 * @see com.demo2do.core.persistence.support.QueryBuilder#getOrder()
	 */
	public QueryOrder getOrder() {
        if (queryOrder == null) {
            queryOrder = new QueryOrder();
        }
        return queryOrder;	
    }

	/* (non-Javadoc)
	 * @see com.demo2do.core.persistence.support.QueryBuilder#buildSentenceTemplate()
	 */
	public String buildSentenceTemplate() {
        return "FROM Set WHERE disabled = false " + getPreparedFilter(false).clauseSentence();
	}

	/* (non-Javadoc)
	 * @see com.demo2do.core.persistence.support.QueryBuilder#buildParameters()
	 */
	public Map<String, Object> buildParameters() {
        return getPreparedFilter(false).parameters();
	}

	/**
	 * @return the code
	 */
	public String getCode() {
		return code;
	}

	/**
	 * @param code the code to set
	 */
	public void setCode(String code) {
		this.code = code;
	}

	/**
	 * @return the country
	 */
	public String getCountry() {
		return country;
	}

	/**
	 * @param country the country to set
	 */
	public void setCountry(String country) {
		this.country = country;
	}

	/**
	 * @return the supplier
	 */
	public String getSupplier() {
		return supplier;
	}

	/**
	 * @param supplier the supplier to set
	 */
	public void setSupplier(String supplier) {
		this.supplier = supplier;
	}

	/**
	 * @return the payType
	 */
	public SetPayType getPayType() {
		return payType;
	}

	/**
	 * @param payType the payType to set
	 */
	public void setPayType(SetPayType payType) {
		this.payType = payType;
	}

    /**
     * @return
     */
    public SetStatus getStatus() {
        return status;
    }

    /**
     * @param status
     */
    public void setStatus(SetStatus status) {
        this.status = status;
    }

}
